<?php
namespace app\system\controller;

/**
 * 后台公用继承器
 *
 * @ author zy <741599086@qq.com>
 * */
 
class Statistics extends Common
{

    /**
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
	public function index()
	{
        if (request()->isAjax()) {
            $result = (new Statistics('related_achievement'))->take_out();
            $list = [];
            foreach ($result as $k => $v) {
                switch ($k) {

                    case "group_sum":
                        $list['总收入(￥)'] = $v;
                        break;
                    // case "group_sum_alipay":
                    //     $list['拼团专区总收入(支付宝)(￥)'] = $v;
                    //     break;
                    // case "group_expend":
                    //     $list['拼团专区总支出(￥)'] = $v;
                    //     break;
                    case "out_sum":
                        $list['总支出(￥)'] = $v;
                        break;
                }   
            }
            $table = [
                [
                    'table' => 'member',
                    'where' => ['is_active' => 0],
                    'type' => 2,
                    'filed' => 'id',
                    'time' => 'create_time',
                    'title' => '普通会员(人)',
                    'sort' => 5
                ],
                [
                    'table' => 'member',
                    'where' => ['level_id' => 2],
                    'type' => 2,
                    'filed' => 'id',
                    'time' => 'create_time',
                    'title' => '拼商(人)',
                    'sort' => 4
                ],
                [
                    'table' => 'member',
                    'where' => ['level_id' => 3],
                    'type' => 2,
                    'filed' => 'id',
                    'time' => 'create_time',
                    'title' => '合伙人(人)',
                    'sort' => 4
                ],
                [
                    'table' => 'member',
                    'where' => ['level_id' => 4],
                    'type' => 2,
                    'filed' => 'id',
                    'time' => 'create_time',
                    'title' => '运营中心(人)',
                    'sort' => 4
                ],
                [
                    'table' => 'draw',
                    'where' => ['state' => 2],
                    'type' => 1,
                    'filed' => 'total',
                    'time' => 'create_time',
                    'title' => '提现金额(￥)',
                    'sort' => 6
                ],
                /* [
                    'table' => 'order_group',
                    'where' => ['pay_state' => 2,'state'=>1],
                    'type' => 2,
                    'filed' => 'id',
                    'time' => 'create_time',
                    'title' => '拼团总订单(条)',
                    'sort' => 6
                ], */
            ];
            $list = array_merge($this->statistics($table), $list);

            $ratio = [
                [
                    'divisor' => ['总支出(￥)'],
                    'b_divisor' => ['总收入(￥)'],
                    'title' => '拨比率(%)'
                ],
            ];
            $list = array_merge($this->ratio($list, $ratio), $list);

            return json(['code' => 1, 'result' => $list]);
        return $this->fetch();
	
	}

    /**
     * @param string $keywords
     * @param int $type
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function recharge($keywords='', $type=-1)
    {
        $keywords = trim($keywords);
        $str = '?';
        $where = [];
        if (!empty($keywords)) {
            $where['u.tel|u.name'] = ['like', "%$keywords%"];
            $str .= 'keywords='.$keywords;
        }
        if ($type != -1) {
            $where['u.user_type'] = $type;
            $str .= $str != '?' ? '&' : '';
            $str .= 'type='.$type;
        }
        $pageconfig['path'] = url($str);

        $where['d.user_type'] = 0;
        $where['d.type'] = 1;
        $userModel = new \app\common\model\User();
        $list = $userModel
            ->alias('u')
            ->join('__DETAILS__ d', 'u.id=d.user_id')
            ->where($where)
            ->field('u.name,u.tel,u.user_type,d.amount,d.add_time')
            ->order('d.id desc')
            ->paginate(20, false, $pageconfig);

        $assign = [
            'amount' => $userModel->sum('jbalance'),
            'list' => $list,
            'typeList' => $this->typeList
        ];
        return $this->fetch('', $assign);
    }


}
